<?php
header("Content-Type: text/html; charset=iso-8859-1");
class historique{
	
	var $fields;
	
	function historique($id){
		$fields = $this->fields();
		$q = "select * from historique where id = '$id'";
		$r = ExecRequete($q);
		if(!mysql_num_rows($r)){
			foreach($fields as $field => $type){
				$this->$field = "";
			}
		}
		else{
			$s = LigneSuivante($r);
			foreach($fields as $field => $type){
				$this->$field = str_replace(chr(146),chr(39),$s->$field);
			}
		}	
	}
	function fields(){
		return describe('historique');
	}
	
	function insert(){
		$q = "insert into historique (valide) values (0)";
		$r = ExecRequete($q);
		$id = mysql_insert_id();
		$this->id = $id;
		if($this->journal && $this->corps && $this->titre && $this->periode && $this->date){
			$rand = (ereg("img:(tmp_[0-9]*):",$this->corps,$regs)) ? $regs[1] : "";
			$this->corps = preg_replace("/img:(tmp_[0-9]*):/","img:$this->id:",$this->corps);
			$texte = ($this->valide) ? "historique publi&eacute;e" : "historique mise en attente de publication";
			if($this->update()){
				if($rand){
					rename("images/historique/$rand","images/historique/$this->id");
				} 
			}  
			return $texte;
		}
		else return "Soit le contenu, soit le titre est manquant";
	}
	function update(){
		$fields = $this->fields();
		$q = "replace historique set ";
		foreach($fields as $field => $type){
			if($this->$field == 'now()') $q .= $field." = ".$this->$field.", ";
			elseif(!get_magic_quotes_gpc()) $q .= $field." = '".(addslashes($this->$field))."', ";
			else $q .= $field." = '".$this->$field."', ";
		}
		if(strstr($q,",") !== false){
			$q = substr($q,0,-2);
			if(ExecRequete($q)) return 1;
		}
		else return 0;			

	}
	
	function delete(){
		if(is_dir("images/historique/$this->id")) rmdirtree("images/historique/$this->id");
		$q = "delete from historique where id = '$this->id' limit 1";
		if(ExecRequete($q)) return 1;
		else return 0;
	}
}
?>
